<?php

namespace yiike\goods\model;

//use chenkby\region\Region;
use Yii;
use \yiike\goods\model\base\Goods as BaseGoods;
use yii\helpers\ArrayHelper;
use trntv\filekit\behaviors\UploadBehavior;
/**
 * This is the model class for table "ims_ttyc_goods".
 */
class Goods extends BaseGoods
{

    public $tags;
    public $imgarr;
    public $province;
    public $city;
    public $district;
    public $shareImg;
    public $param_name;
    public $param_val;
    public $spec_img;
    //public $is_spec;
    public function behaviors()
    {
        return ArrayHelper::merge(
            parent::behaviors(),
            [
                [
                    'class' => UploadBehavior::className(),
                    'attribute' => 'imgarr',
                    // 'pathAttribute' => 'image',
                ],
                [
                    'class' => UploadBehavior::className(),
                    'attribute' => 'shareImg',
                    'pathAttribute' => 'share_image',
                ],
            ]
        );
    }

    public function rules()
    {
        return [
            [['title', 'price', 'content', 'quan_rule'], 'required'],
            [['weid', 'type', 'top', 'orderby', 'cid', 'ccid', 'store_id', 'sold', 'full_num_shipping', 'invoice', 'status', 'sale_time_start', 'sale_time_end', 'hide_search', 'no_return', 'weight', 'stock', 'stock_rule', 'is_spec', 'max_buy', 'min_buy', 'quan_rule', 'give_credit1'], 'integer'],
            [['image', 'no_shipping', 'property', 'content'], 'string'],
            [['price', 'orig_price', 'cost_price', 'full_money_shipping', 'shipping_fee', 'commision'], 'number'],
            [['title', 'goods_sn'], 'string', 'max' => 100],
            [['sub_title'], 'string', 'max' => 200],
            [['short_title', 'keyword', 'tag', 'location_p', 'location_c'], 'string', 'max' => 20],
            [['logo', 'share_image'], 'string', 'max' => 250],
            [['imgarr', 'shareImg', 'image', 'cid', 'weid'], 'safe'],
            [['share_title', 'share_content'], 'string', 'max' => 50],
            [['cid'], 'exist', 'skipOnError' => true, 'targetClass' => \yiike\goods\model\GoodsCategory::className(), 'targetAttribute' => ['cid' => 'id']],
            [['store_id'], 'exist', 'skipOnError' => true, 'targetClass' => \yiike\goods\model\Stores::className(), 'targetAttribute' => ['store_id' => 'id']]
        ];
    }

    function beforeSave($insert)
    {


        if (!empty($_POST['param_name'])) {
            foreach ($_POST['param_name'] as $k => $v) {
                $paramArr[$k]['name'] = $v;
                foreach ($_POST['param_val'] as $m => $n) {
                    $paramArr[$k]['val'] = $_POST['param_val'][$k];
                }
            }

            $property = json_encode($paramArr, JSON_UNESCAPED_UNICODE);
        } else {
            $property = '';
        }
        foreach ($this->imgarr as $k => $v) {
            $arr[$k] = $v['path'];
        }

        $this->logo = $arr[0];
        $arr = implode('|', $arr);

        $this->image = $arr;
        $tags = $_POST['Goods']['tags'];

        if (!empty($tags)) {
            $tags = implode(',', $tags);
        } else {
            $tags = '';
        }

        if (!empty($_POST['is_spec']) and $_POST['is_spec'] == 1) {
            $is_spec = 1;
        } else {
            $is_spec = 0;
        }
        //$region = new Region();

        $this->tag = $tags;
        $this->property = $property;
        $this->is_spec = $is_spec;
        //$this->store_id = 174;
        $this->ccid = 0;
        $this->weid = 2;

        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }

    function afterFind()
    {
        $arr = explode('|', $this->image);
        foreach ($arr as $k => $v) {
            $images[$k]['path'] = $v;
            $images[$k]['base_url'] = Yii::getAlias('@storageUrl/source');
        }
        $tags = explode(',',$this->tag);
        $this->tags = $tags ;
        $this->imgarr = $images;
        parent::afterFind(); // TODO: Change the autogenerated stub
    }
}
